/*
 * Tencent is pleased to support the open source community by making Tinker available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yylending.las.tinker.Log;

import com.tencent.tinker.lib.util.TinkerLog;
import com.yyfax.common.CLog;

/**
 * Created by zhangshaowen on 16/6/3.
 */
public class TinkerLogImpl implements TinkerLog.TinkerLogImp {
	private static final String TAG = "Tinker.TinkerLogImpl";

	public static final int LEVEL_VERBOSE = 0;
	public static final int LEVEL_DEBUG = 1;
	public static final int LEVEL_INFO = 2;
	public static final int LEVEL_WARNING = 3;
	public static final int LEVEL_ERROR = 4;
	public static final int LEVEL_NONE = 5;
	private static int mLevel = LEVEL_VERBOSE;

	public static int getLogLevel() {
		return mLevel;
	}

	public static void setLevel(final int level) {
		mLevel = level;
		CLog.w(TAG, "new log level: " + level);
	}

	@Override
	public void v(String s, String s1, Object... objects) {
		if (mLevel <= LEVEL_VERBOSE) {
			final String log = objects == null ? s1 : String.format(s1, objects);
			CLog.v(s, log);
		}
	}

	@Override
	public void i(String s, String s1, Object... objects) {
		if (mLevel <= LEVEL_INFO) {
			final String log = objects == null ? s1 : String.format(s1, objects);
			CLog.i(s, log);
		}
	}

	@Override
	public void w(String s, String s1, Object... objects) {
		if (mLevel <= LEVEL_WARNING) {
			final String log = objects == null ? s1 : String.format(s1, objects);
			CLog.w(s, log);
		}
	}

	@Override
	public void d(String s, String s1, Object... objects) {
		if (mLevel <= LEVEL_DEBUG) {
			final String log = objects == null ? s1 : String.format(s1, objects);
			CLog.d(s, log);
		}
	}

	@Override
	public void e(String s, String s1, Object... objects) {
		if (mLevel <= LEVEL_ERROR) {
			final String log = objects == null ? s1 : String.format(s1, objects);
			CLog.e(s, log);
		}
	}

	@Override
	public void printErrStackTrace(String s, Throwable throwable, String s1, Object... objects) {
		String log = objects == null ? s1 : String.format(s1, objects);
		if (log == null) {
			log = "";
		}
		log = log + "  " + android.util.Log.getStackTraceString(throwable);
		CLog.e(s, log);
	}
}
